package com.link.base.base.chiper.service;

import javax.annotation.Resource;

import com.link.core.util.ChiperUtil;
import com.link.core.util.RedisUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import com.link.base.base.chiper.dao.mybatis.mapper.ChiperWriteMapper;
import org.springframework.stereotype.Service;
import com.link.base.base.chiper.model.ChiperWrite;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;


import java.lang.reflect.Field;
import java.util.List;

/**
 * 针对插入类的字段隐藏加密操作
 *
 * @author: 郑洪达
 * @date: 2019/12/12
 */
@Service
public class ChiperWriteServiceImpl extends BasicServiceImpl<ChiperWrite> implements ChiperWriteService {

    @Resource
    private ChiperWriteMapper chiperWriteMapper;
    
    @Override
    public BasicMapper<ChiperWrite> getBasicMapper() {
        return chiperWriteMapper;
    }

    @Override
    public void clearCache(ChiperWrite chiperWrite) throws Exception {
        LinkRedisClient jedis = null;
        try {
            List<ChiperWrite> chiperWriteList = chiperWriteMapper.queryByExamplePage(chiperWrite);
            jedis = RedisUtil.getJedis();
            jedis.del(ChiperUtil.LNK_CHIPER_WRITE_KEY);
            for (ChiperWrite chiperWriteTemp : chiperWriteList) {
                if (("Y").equals(chiperWriteTemp.getValid()) && !("None").equals(chiperWriteTemp.getAction())) {
                    String  chiperWriteInfo = chiperWriteTemp.getClassName() + ";" + chiperWriteTemp.getFieldName()
                         + ";" + chiperWriteTemp.getAction();
                    jedis.lpush(ChiperUtil.LNK_CHIPER_WRITE_KEY, chiperWriteInfo);
                }
            }
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    @Override
    public boolean checkChiper(ChiperWrite chiperWrite) throws Exception {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Class clazz = loader.loadClass(chiperWrite.getClassName());
        //需要检测的属性名称
        String fieldname = chiperWrite.getFieldName();
        return isFieldExist(clazz,fieldname);
    }

    private static boolean isFieldExist(Class clazz, String targetFieldName) throws Exception {
        boolean exist = false;
        // 获取所有的属性数组
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            // 属性名称
            String currentFieldName = field.getName();
            if (currentFieldName.equals(targetFieldName)) {
                exist = true;
                break;
            }
        }
        // 获取继承类里面的属性值
        Class superClass = clazz.getSuperclass();
        if (null != superClass) {
            Field[] superFields = superClass.getDeclaredFields();
            for (Field superField : superFields) {
                // 属性名称
                String currentFieldName = superField.getName();
                if (currentFieldName.equals(targetFieldName)) {
                    exist = true;
                    break;
                }
            }
        }
        return exist;
    }

}

